<!--
 * @Description: 交工数据标准
 * @Autor: yjx
 * @Date: 2021-11-19 09:54:54
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-01-04 17:49:51
-->
<template>
    <div class="due_data_standard">
        <div v-if="config.nameId">
            <div class="top">
                <div class="btn">
                    <el-button size="mini" class="bottom_all"  @click="handleSave">保 存</el-button>
                </div>
            </div>

            <div class="content">
                <Table :tableConfig="tableConfig" :tableData="tableData" />
            </div>
        </div>
        <div v-else class="text">暂无数据</div>
    </div>
</template>
<script>
import Table from '@/views/platform/ICPS/smart/schedul/components/BlockTable.vue'
import { saveOrUpdate3, queryStand } from "@/api/qualityTemplate"

export default {
    components: {
        Table,
    },
    props: {
        index: {
            type: String,
            default: ()=>''
        },
        config: {
            type: Object,
            default: ()=>{}
        }
    },
    watch: {
        config: {
            deep: true,
            handler: function(newVal) {
                const params = {
                    companyId: newVal.companyId,
                    nameId: newVal.nameId,
                    specificationId: newVal.specificationId,
                    type: 2
                }
                queryStand(params).then(res=>{
                    if(res && res.code === 1001 && res.data && res.data.content) {
                        this.tableData = JSON.parse(res.data.content)
                    } else {
                        this.tableData = this.$options.data().tableData;
                    }
                })
            }
        }
    },
    data() {
        return {
            tableConfig: [
                {label: '检验项目', prop: 'name'},
                {label: '下限值', prop: 'min', render:({row})=>(row.type === 'input' ? <el-input v-model={row.min} type="number" placeholder="请输入" /> : '/')},
                {label: '上限值', prop: 'max', render:({row})=>(row.type === 'input' ? <el-input v-model={row.max} type="number" placeholder="请输入" /> : '/')},
            ],
            tableData: [
                {type: 'input', name: '破坏荷载1', min: '', max: ''},
                {type: 'input', name: '破坏荷载2', min: '', max: ''},
                {type: 'input', name: '破坏荷载3', min: '', max: ''},
                {type: 'input', name: '抗压强度单块值1', min: '', max: ''},
                {type: 'input', name: '抗压强度单块值2', min: '', max: ''},
                {type: 'input', name: '抗压强度单块值3', min: '', max: ''},
                {type: 'input', name: '抗压强度代表值', min: '', max: ''},
            ],
        }
    },
    methods: {
        handleSave() { // 保存
            const params = {
                ...this.config,
                projectList: this.formatData(),
                content: JSON.stringify(this.tableData)
            }
            saveOrUpdate3(params).then(res=>{
                if(res && res.code === 1001) {
                    this.$message.success('保存成功')
                }
            })
        },
        formatData() { // 格式化表格数据，处理成后端需要的格式
            const arr = [];
            this.tableData.forEach((item, index)=>{
                if(item.type === 'select') {
                    arr.push({
                        checkProject: item.name,
                        checkProjectId: index,
                        dataType: 1, //数据类型：数据类型：1下拉框2数值范围3输入框
                        requirement: item.select ? item.select.value : '',
                        requirementValue: item.select ? item.select.label : '',
                        requirementCondition: JSON.stringify(item.options)
                    })
                }
                if(item.type === 'input') {
                    arr.push({
                        checkProject: item.name,
                        checkProjectId: index,
                        dataType: 2, //数据类型：数据类型：1下拉框2数值范围3输入框
                        lowerLimit: item.min,
                        upperLimit: item.max
                    })
                }
            })
            return arr
        },
    },
}
</script>
<style lang="scss" scoped>
.due_data_standard {
    box-sizing: border-box;
    width: 100%;
    padding: 20px;
    .top {
        display: flex;
        align-items: center;
        padding-bottom: 20px;
        .select {
            display: flex;
            align-items: center;
        }
    }
    .content {
        width: 100%;
        height: 430px;
        overflow: auto;

    }
    .text {
        width: 100%;
        height: 500px;
        color: #ccc;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>


